package br.edu.ufcg.dsc.actions.marc;

import java.util.List;
import java.util.Vector;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import br.edu.ufcg.dsc.domain.marc.DataField;
import br.edu.ufcg.dsc.domain.marc.MARCResource;
import br.edu.ufcg.dsc.services.ResourceService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Result(name="success", type=ServletDispatcherResult.class, value="/nodecorate/preenchedor.jsp")
@ParentPackage("myPackage")
@SuppressWarnings("serial")
public class SimpleToMarcAction extends ActionSupport implements  Preparable {
	
	private int resourceId;
	
	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public List<String> getResults() {
		List<String> results = new Vector<String>();
	
		if (initial != null) {
			results.add("tag_000_subfield_00:" + initial.getLeader());
			
			for (String key : initial.getControlFields().keySet()) {
				results.add("tag_" + key + "_subfield_00:" + initial.getControlFields().get(key));
			}
			
			for (String field : initial.getDataFields().keySet()) {
				
				DataField value = initial.getDataFields().get(field);
				
				results.add("tag_" + field.substring(0, 3) + "_indicator1:" + value.getIndicator1());
				results.add("tag_" + field.substring(0, 3) + "_indicator2:" + value.getIndicator2());
				
				for (String key : value.getSubfields().keySet()) {
					results.add("tag_" + field.substring(0, 3) + "_subfield_" + key + ":" + value.getSubfields().get(key));
				}
			}
		}
		return results;
	}
	
	private MARCResource initial;
	
	public void prepare() throws Exception {
		if (ResourceService.find(resourceId) != null) {
			initial = ResourceService.find(resourceId).getMarc();
		} else {
			initial = null;
		}
	}
}
